<?php
namespace App\Services;
use GuzzleHttp\Client;
use App\Services\WechatoffiaccountService;
use Log,Exception;

class WechattmplmsgService
{
    /**
    * 微信模版消息推送
    */
    public static function sendSingleTempleteMsg($template_id,$wechat_openid,$url,$miniprogram_appid,$miniprogram_pagepath,$paramas)
    {
        if (empty($wechat_openid)) return ["status"=>"error","msg"=>"用户OPENID不能为空!"];
        $url = empty($url) ? "https://bab.moreqifu.cn/admin/prices/products_lists_pay" : $url;
        $offapp = WechatoffiaccountService::officialAccount();
        if (empty($miniprogram_appid)) {
            $result = $offapp->template_message->send([
                'touser' => $wechat_openid,
                'template_id' => $template_id,
                'url' => $url,
                'data' => $paramas,
            ]);
        } else {
            $result = $offapp->template_message->send([
                'touser' => $wechat_openid,
                'template_id' => $template_id,
                'url' => $url,
                'miniprogram' => [
                    'appid' => $miniprogram_appid,
                    'pagepath' => $miniprogram_pagepath,
                ],
                'data' => $paramas,
            ]);
        }
        return $result;
    }

    public static function sendMemberTempleteMsg($wechat_openid,$username,$product_id,$start_time,$end_time)
    {
        if (empty($wechat_openid)) return ["status"=>"error","msg"=>"用户OPENID不能为空!"];
        $products = ["","免费版","基础版","高级版","旗舰版"];
        $offapp = WechatoffiaccountService::officialAccount();
        $result = $offapp->template_message->send([
            'touser' => $wechat_openid,
            'template_id' => 'ct52Xir8YPDfODcDiuPKatsq0aD8U8qo8hrbf8XE7yM',
            'url' => 'https://bab.moreqifu.cn/admin/prices/products_lists_pay',
            'miniprogram' => [
                'appid' => 'wx820e151d0905ab1a',
                'pagepath' => 'pages/users/users_member',
            ],
            'data' => [
                'first' => '天天外链会员开通成功提醒',
                'keyword1' => '天天外链会员('.$products[$product_id].')',
                'keyword2' => $username,
                'keyword3' => $start_time,
                'keyword4' => $end_time,
                'remark' => '请注意会员到期时间，并且及时续费！',
            ],
        ]);
        return $result;
    }
}
